<?php
namespace Mob \ Model;

use Think \ Model;
use Think \ Hook;
/**导员模型
 * Class InfoModel
 */

require_once ('./Application/Mob/Common/function.php');

class TeacherModel extends Model {


  public function creator($teacherUid,$teacherOpenId,$teacheName,$deptId,$classGrade,$schooId,$level=1)
  {
  $data['teacherUid']=$teacherUid;
  $data['teacherOpenId']=$teacherOpenId;
  $data['teacherName']=$teacheName;
  $data['status']=0;
  $data['schooId']=$schooId;
  $data['deptId']=$deptId;
  $data['classGrade']=$classGrade;
  $data['level']=$level;
  return $this->add($data);

  }
  public function is_teacher($uid)
  {

  	$where = array('teacherUid'=>$uid,'status'=>1);
  	return $this->where($where)->find();

  }
  public function query_Teacher($uid)
  {
  $where = array('teacherUid'=>$uid,'status'=>1);
  	return $this->where($where)->field('teacherUid,teacherOpenId,teacherName,schooId,deptId,level')->find();

  }
  public function get_TeacherClass($uid,$schooId='10183')
  {
  	$orga = M('Orga');

  	// $where = array('teacherId'=>$uid,'schooId'=>$schooId,'status'=>1);
  	$where = "(teacherId =$uid or leaveApprovalUid = $uid ) and schooId = $schooId and status =1 ";
    $orga= $orga->where($where)->field('teacherId,leaveApprovalUid,orgaId,orgaName,schooId,desc,orgaType,desc,superId,sortId,superSortId,status')->order('orgaId,orgaType,sortId,superSortId')->select();

    $count=1;
    foreach ($orga as &$v)
    {
    $v['sortId']=$count;
    $count++;

    }
    return $orga;

  }

  public function get_ClassGroupUid($teacherId,$schooId='10183')
  {
    $teacher = new \ Think \ Model();
   return $teacher->query("select s.Uid,s.name,o.orgaId,o.organame,o.teacherId,s.studentId from ocenter_student s,ocenter_orga o where s.ClassId = o.orgaId and o.orgaType =3 and teacherId = $teacherId and s.verificFlag =1 and s.schooId = o.schooId and s.schooId =$schooId");

  }

  public function get_AllTeacher($schooId='10183')
  {
   $where = array('schooId'=>$schooId,'status'=>1);

  }
  public function get_TeacherInfo($teacherId)
  {
   return $this->where(array('teacherUid'=>$teacherId))->field('teacherUid,teacherOpenId,teacherName,status,schooId,approvalUid,level')->find();

  }

  public function get_MyClassStudentRecord($teacherId,$schooId='10183',$type=1)
  {

   $classInfo = $this->get_MyClass($teacherId,$schooId,$type);
  foreach($classInfo as &$v)
  {
   foreach($v['Class'] as &$l)
  {
  $l['ClassState']=D('Student')->get_ClassRecord($l['orgaId'],$schooId='10183');
  }

  }

  unset($v);

  return $classInfo;



  }

  public function get_MyClassSignRecord($teacherId,$schooId='10183',$type=1)
  {
  $classInfo = $this->get_MyClass($teacherId,$schooId,$type);
  foreach($classInfo as &$v)
  {
   foreach($v['Class'] as &$l)
  {
  $l['signState']=D('Sign')->get_ClassSignStatus($l['orgaId'],$v['schooId']);
  }
  }
  unset($v);
  return $classInfo;

  }

  public function get_MyClass($teacherId,$schooId='10183',$type=1)
  {

  $teacher = new \ Think \ Model();
  if($type==1)
  {
  $classInfo = $teacher->query("select o.orgaId as orgaId,o.orgaName,o.schooId from ( select  distinct(superId) from ocenter_orga o where (teacherId = $teacherId or leaveApprovalUid = $teacherId ) " .
  		"and schooId = $schooId and status =1 ) g,ocenter_orga o where g.superId = o.orgaId and orgaType = 2");
  }elseif($type==2)
  {
  $classInfo = $teacher->query("select a.orgaId as orgaId,a.orgaName,a.schooId from ocenter_orga a,( select distinct(superId) as superId from ocenter_orga where teacherId in
(select teacherUid from ocenter_teacher where  deptid in  (select deptId from ocenter_dept where  schooId = $schooId and status = 1 and  manageUid = $teacherId) and schooId = $schooId and status = 1)) b
where a.orgaId = b.superId and a.orgaType = 2");
  }
  $count=1;
  foreach($classInfo as &$v)
  {
  $v['sortId']=$count;
  $orgaId = $v['orgaId'];
  $v['Class'] =$teacher->query("select  orgaId,orgaName,superId from ocenter_orga o where superId = $orgaId  and status = 1 and schooId = $schooId");

  $count++;
  }
  unset($v);
  return $classInfo;

  }

  public function get_teacherDetail($manageId,$schooId)
  {
  $teacher = new \ Think \ Model();
  return $approvalRecord = $teacher->query("select t.schooId,t.teacherUid,t.teacherName,t.creatorTime,t.status,d.deptId from  ocenter_teacher  t,(select deptId from ocenter_dept where  schooId = $schooId and status =1
   and  manageUid = $manageId) d where t.schooId = $schooId  and t.status in (1,0) and t.deptId = d.deptId and level=1");
  }

  public function approvalVerify($teacherId,$verifyFlg,$manageId,$schooId=10183)
  {
   $res =  $this->where(array('teacherUid'=>$teacherId,'schooId'=>$schooId,'status'=>0))->save(array('status'=>$verifyFlg,'approvalUid'=>$manageId));
   if($verifyFlg==1&&$res)
   {
     M('member')->where(array('uid'=>$teacherId,'status'=>1))->save(array('usertype'=>2));
   }
   return $res;

  }
  public function removeOperate($teacherUid,$manageUid,$schooId)
  {
  $res =$this->where(array('teacherUid'=>$teacherUid,'schooId'=>$schooId,'status'=>1))->delete();
  if($res)
  {
  $data['studentUid']=$teacherUid;
  $data['operateTime']=time();
  $data['operateFlg']=1;
  $data['operateType']=3;
  $data['operateUid']=$manageUid;
 return M('removerecord')->add($data);
  }
  }
  //获取待审核学校主任
  public function get_waitAuditManage($schooId=10183)
  {
  return $this->where("schooId = $schooId  and status in (0,1) and level =2")->field('teacherUid,teacherName,status,schooId,creatorTime,deptId,classGrade')->select();


  }

















}